package workflow;

import java.util.*;

import thread.ScrapeThread;

public class ScraperManager {
	
	String[] articles = {"892","838","2559","2555","2552","2548","2543",
			"2532","2522","2513","2500","2494","2489","2487","2482",
			"2479","2475","2473","2470","2466","2464","2460","2457",
			"2453","2450","2447","2445","2442","2440","2438"};
	
	private boolean exception = false;
	private int threadCount = 0;
	private List<String> list = new ArrayList<String>();
	private final int MAX = 12;
	
	public static void main (String args[]){
		try{
			String out = new ScraperManager().doIt("the%20messenger");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public String doIt(String poster) throws Exception {
		for(int i=0;i<MAX;i++){
			new ScrapeThread(this,articles[i],poster).start();;
		}
		while(true){
			Thread.sleep(5000);
			if(threadCount==MAX)break;
		}
		StringBuffer sb = new StringBuffer();
		for(String l:list){
			sb.append(l);
		}
		return sb.toString();
	}
	
	public synchronized void callBack(String s){
		list.add(s);
		threadCount++;
	}
	
	public synchronized void callBackException(Exception e){
		exception = true;
		e.printStackTrace();
	}
	
}
